 // 引入express模块
const express = require('express');
// 引入连接池模块
const pool = require('../pool.js')
// 创建路由器对象
const router = express.Router();

const jwt = require('../jwt')

//------------------------用户注册-----------------------------
// http://127.0.0.1:3000/user/register
//let isRegistered = false; //设置一个开关，设置为flase，默认用户没有注册
router.post('/register', (req, res, next) => {
    //获取post传递的参数
    var obj = req.body
    console.log(obj);
    pool.query('select * from user where phone=?',
    [obj.phone], (err, result) => {
            if (err) {
                next(err);
                return;
            }
            console.log(result);
            if (result.length !== 0) {
                res.send({
                    code: 202,
                    msg: '账户已经存在，请直接登录'
                });
            } else {
                pool.query("insert into user (uname,upwd,phone) values(?,?,?)",
                    [obj.uname, obj.upwd, obj.phone], (err, result) => {
                        if (err) {
                            next(err);
                            return;
                        }
                       
                        else{
                            res.send(
                                {
                                    code:200,
                                    msg:"注册成功"
                                }

                                )
                        }
                    })
            }
        })
    //  pool.query('insert into user set?',[obj],(err,result)=>{
    //      if(err){
    //          next(err);
    //          return;
    //      }
    //      console.log(result);
    //      res.send({code:200,msg:'注册成功'});
    //  })
});
// --------------------------用户登录----------------------
// http://127.0.0.1:3000/user/login
router.post('/login', (req, res, next) => {
    var obj = req.body
    console.log(obj);
    if (!obj.phone) {
        res.send({
            code: 401,
            msg: '用户名手机号不能为空'
        });
        return;
    }
    pool.query('select * from user where phone=? and upwd=?',
        [obj.phone, obj.upwd], (err, result) => {
            if (err) {
                next(err);
                return;
            }
            console.log(result);
            if (result.length === 0) {
                res.send({
                    code: 500,
                    msg: '登录失败'
                });
            } else {
                // 将登录的密码进行编译成token密码！！！！！
                res.send({
                    code: 200,
                    msg: '登录成功',
                    data: result[0],
                    // 登录成功后保存token
                    // generateToken从jwt.js引入生成token
                    token: jwt.generateToken(result[0]) //登录成功
                });
            }
            
        })
});




// ---------------------------删除用户-------------------------
// http://127.0.0.1:3000/user/+不再是拼接/:id，是加上需要删除的编号
router.post('/:aid', (req, res, next) => {
    var obj = req.params;
    console.log(obj);
    // order是数据库的关键词必须要加   ``   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    pool.query('delete from `order` where aid=?', [obj.aid], (err, result) => {
        if (err) {
            next(err);
            return;
        }
        console.log(result);
        // affectedRows===0说明没有数据被删除,则是删除失败
        if (result.affectedRows === 0) {
            res.send({
                code: 500,
                msg: '删除失败'
            });
        } else {
            res.send({
                code: 200,
                msg: '删除成功'
            });

        }
    })
});

// 暴露出去
module.exports = router